package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.SiteConfig;

import admin.forms.SiteConfigForm;
import admin.models.SiteConfigModel;

public class SiteConfigServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		TreeMap check = new TreeMap();
		
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		long id = 1;
		SiteConfigModel siteConfigModel = SiteConfigModel.getInstance();
		
		SiteConfig configs = siteConfigModel.getConfig(id);
		
		if (configs == null) {
			configs = new SiteConfig();
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("configs", configs);
		try {
			request.getRequestDispatcher("/admin/pages/" + getPageJsp(name) + ".jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		//Get config name
		String path = ((HttpServletRequest) request).getRequestURI();
		String name = URLDecoder.decode(path.toLowerCase().replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		long id = 1;
		SiteConfigModel siteConfigModel = SiteConfigModel.getInstance();
		SiteConfig configs = siteConfigModel.getConfig(id);
		if (configs == null) {
			configs = new SiteConfig();
			configs.setId(id);
		}
		
		//Check valid and get value form
		TreeMap forms = getFormValid(name, request, configs);
		configs = (SiteConfig) forms.get("config");
		TreeMap check = (TreeMap) forms.get("checkValid");
		
		if (check.size() == 0) {
			siteConfigModel.insert(configs);
			session.setAttribute("success", "Update success site config");
		}
		
		request.setAttribute("form", "edit");
		request.setAttribute("check", check);
		request.setAttribute("configs", configs);
		
		try {
			request.getRequestDispatcher("/admin/pages/" + getPageJsp(name) + ".jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getPageJsp(String name) {
		String page = "form_site_config";

		if (name.equalsIgnoreCase("layout")) {
			page = "form_layout_config";
		}
		
		return page;
	}
	
	private TreeMap getFormValid(String name, HttpServletRequest request, SiteConfig configs) {
		TreeMap forms = new TreeMap();
		
		if (name.equalsIgnoreCase("layout")) {
			forms = SiteConfigForm.checkValidLayout(request, configs);
		} else {
			forms = SiteConfigForm.checkValidSite(request, configs);
		}
		
		return forms;
	}
}
